package org.zjvis.datascience.service.cleanup;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.zjvis.datascience.common.constant.Constant;
import org.zjvis.datascience.common.exception.DataScienceException;
import org.zjvis.datascience.service.dataprovider.GPDataProvider;

import javax.servlet.ServletContext;
import java.sql.Connection;
import java.sql.Statement;

import static org.zjvis.datascience.common.constant.DatabaseConstant.DEFAULT_DATASET_ID;

/**
 * @description 清洗操作 推荐历史记录清除线程
 * @date 2021-10-15
 */
@Data
@Component
public class CleanUpRecommendTablesRunner implements Runnable {
    private final static Logger logger = LoggerFactory.getLogger("CleanUpRecommendTablesRunner");

    /**
     * 待删除的gp表名 ","分隔
     */
    private String tables;
//
    private GPDataProvider gpDataProvider;

    @Autowired
    private ServletContext servletContext;

    public static CleanUpRecommendTablesRunner init(String tables) {
        CleanUpRecommendTablesRunner cleanUpRecommendTablesRunner = new CleanUpRecommendTablesRunner();
        cleanUpRecommendTablesRunner.setTables(tables);
        return cleanUpRecommendTablesRunner;
    }

    @Override
    public void run() {
        if (StringUtils.isNotEmpty(tables)) {
//            gpDataProvider.dropRedundantTables(tables, false);
            dropRedundantTables(tables, false);
            logger.info(String.format("CleanUpRecommendTablesRunner cleanup recommendTables task finish. tables=%s", tables));
        }
    }

    private void dropRedundantTables(String tables, boolean isView) {
        String dataSourceKey = (String) servletContext.getAttribute(Constant.DEFAULT_DATA_SOURCE_KEY);
        Connection conn = null;
        try {
            conn = gpDataProvider.getConn(DEFAULT_DATASET_ID);
            Statement st = conn.createStatement();
            for (String table : tables.split(",")) {
                String sql;
                if (isView && table.contains("solid_tclean_")) {
                    sql = String.format("drop table if exists %s cascade", table);
                } else {
                    sql = String
                            .format("drop %s if exists %s cascade", (isView ? "view" : "table"), table);
                }
                st.executeUpdate(sql);
//                gpDataService.executeUpdateSQL(dataSourceKey, sql);
            }
        } catch (Exception e) {
            logger.error("JdbcDataProvider.dropRedundantTables() drop error,tables={}", tables);
            throw new DataScienceException(
                    String.format("get jdbc connection error, since %s", e.getMessage()), e);
        }
        finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {

                }
            }
        }
    }
}
